package com.gg.ggaiagent.demo.invoke;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;

import java.util.ArrayList;
import java.util.HashMap;

public class HttpAiInvoke {
    public static void main(String[] args) {
        // 获取环境变量中的 API Key
        String apiKey = TestApiKey.API_KEY;

        // 构建请求 URL
        String url = "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation";

        // 构建请求头参数
        String authorizationHeader = "Bearer " + apiKey;
        String contentTypeHeader = "application/json";

        // 构建请求体 JSON 数据
        String requestBody = JSONUtil.toJsonStr(new HashMap<String, Object>() {{
            put("model", "qwen-plus");
            put("input", new HashMap<String, Object>() {{
                put("messages", new ArrayList<HashMap<String, String>>() {{
                    add(new HashMap<String, String>() {{
                        put("role", "system");
                        put("content", "You are a helpful assistant.");
                    }});
                    add(new HashMap<String, String>() {{
                        put("role", "user");
                        put("content", "你是谁？");
                    }});
                }});
            }});
            put("parameters", new HashMap<String, String>() {{
                put("result_format", "message");
            }});
        }});

        // 发送 POST 请求
        HttpResponse response = HttpRequest.post(url)
                .header("Authorization", authorizationHeader)
                .header("Content-Type", contentTypeHeader)
                .body(requestBody)
                .execute();

        // 输出响应状态码和响应内容
        System.out.println("Response Status Code: " + response.getStatus());
        System.out.println("Response Body: " + response.body());
    }
}
